package com.sailfish.springbootdemo.service.db1;


import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db1.PcbaDeviceParamDao;
import com.sailfish.springbootdemo.dao.db1.PcbaDeviceParamTypeDao;
import com.sailfish.springbootdemo.dao.db1.PcbaDeviceTypeDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceParam;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceParamType;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceType;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.CommonUtils;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PcbaDeviceParamTypeServiceImpl implements PcbaDeviceParamTypeService {

    @Autowired
    private PcbaDeviceParamTypeDao pcbaDeviceParamTypeDao;

    @Autowired
    private PcbaDeviceParamDao pcbaDeviceParamDao;

    @Autowired
    private PcbaDeviceTypeDao pcbaDeviceTypeDao;

    @Autowired
    private CommonJudgeCanDeleteService commonService;

    @Autowired
    private OperateService operateService;

    @Value("${personal.config.file.file-save-url}")
    private String fileSaveUrl;

    @Value("${personal.config.amazon-s3.url}")
    private String url;
    @Value("${personal.config.amazon-s3.file-bucket}")
    private String fileBucket;
    @Value("${personal.config.env}")
    private String env;

    private String operateSmallType = "基线大类";

    @Override
    public List<PcbaDeviceParamType> getPcbaDeviceParamTypeByPcbaDeviceTypeId(HttpServletRequest request, Integer pcbaDeviceTypeId, String searchValue) {
        List<PcbaDeviceParamType> list = pcbaDeviceParamTypeDao.getPcbaDeviceParamTypeByPcbaDeviceTypeId(pcbaDeviceTypeId, searchValue);
        for (PcbaDeviceParamType pcbaDeviceParamType : list) {
            List<PcbaDeviceParam> byPcbaDeviceParamTypeId = pcbaDeviceParamDao.getByPcbaDeviceParamTypeId(pcbaDeviceParamType.getPcbaDeviceParamTypeId());

            for (PcbaDeviceParam pcbaDeviceParam : byPcbaDeviceParamTypeId) {
                if (env != null && env.contains("pro"))  {
                    pcbaDeviceParam.setPcbaDeviceParamPictureBase64(CommonUtils.base64Prefix() + CommonUtils.getImageBase64FromUrl(url + "/" + fileBucket + "/" + pcbaDeviceParam.getPcbaDeviceParamPicture()));
                } else {
                    if(request != null){
                        pcbaDeviceParam.setPcbaDeviceParamPictureBase64(CommonUtils.base64Prefix() + CommonUtils.getImageBase64FromUrl(CommonUtils.getApiPrefixUrl(request) + "/" + pcbaDeviceParam.getPcbaDeviceParamPicture()));
                    }
                }
            }

            pcbaDeviceParamType.setPcbaDeviceParamList(byPcbaDeviceParamTypeId);

            PcbaDeviceType byId = pcbaDeviceTypeDao.getById(pcbaDeviceParamType.getPcbaDeviceTypeId());
            pcbaDeviceParamType.setPcbaDeviceType(byId);
        }
        return list;
    }

    @Override
    public Result deletePcbaDeviceParamType(Integer pcbaDeviceParamTypeId) {
        try {
            if (pcbaDeviceParamTypeId == null || "".equals(pcbaDeviceParamTypeId.toString())) {
                return ResultUtil.error(500, "缺少参数pcbaDeviceParamTypeId", "Miss Param pcbaDeviceParamTypeId", null);
            }

            String userId = UserHeaderHolder.getUserId();
            PcbaDeviceParamType oldPcbaDeviceParamType = pcbaDeviceParamTypeDao.getPcbaDeviceParamTypeById(pcbaDeviceParamTypeId);

            Map<String, Integer> stringIntegerMap = commonService.judgeCanDeletePcbaDeviceParamType(pcbaDeviceParamTypeId);
            if (stringIntegerMap.get("pcbaDeviceParamNum") > 0) {
                return ResultUtil.error(500,
                        "禁止删除，当前项" + stringIntegerMap.get("pcbaDeviceParamNum") + "处关联基线模板",
                        "Forbidden Delete", null);
            }
            Integer integer = pcbaDeviceParamTypeDao.deletePcbaDeviceParamType(pcbaDeviceParamTypeId);
            if (integer > 0) {
                // 操作日志
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 2, 1, new Date(), JSONObject.toJSONString(oldPcbaDeviceParamType), null));
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result addOrUpdatePcbaDeviceParamType(String mode, PcbaDeviceParamType pcbaDeviceParamType) {
        try {
            if (pcbaDeviceParamType == null) {
                return ResultUtil.error(500, "缺少参数pcbaDeviceParamType", "Miss Param pcbaDeviceParamType", null);
            }
            if (mode == null || "".equals(mode)) {
                return ResultUtil.error(500, "缺少参数mode", "Miss Param mode", null);
            }
            if (!("edit".equals(mode) || "add".equals(mode))) {
                return ResultUtil.error(500, "mode 参数错误", "mode Param Error", null);
            }

            String userId = UserHeaderHolder.getUserId();

            if ("edit".equals(mode)) {
                if (pcbaDeviceParamType.getPcbaDeviceParamTypeId() == null || "".equals(pcbaDeviceParamType.getPcbaDeviceParamTypeId().toString())) {
                    return ResultUtil.error(500, "缺少参数pcbaDeviceParamTypeId", "Miss Param pcbaDeviceParamTypeId", null);
                }

                PcbaDeviceParamType oldPcbaDeviceParamType = pcbaDeviceParamTypeDao.getPcbaDeviceParamTypeById(pcbaDeviceParamType.getPcbaDeviceParamTypeId());

                List<PcbaDeviceParamType> pcbaDeviceParamTypeList = pcbaDeviceParamTypeDao.judgeUniquePcbaDeviceParamType(pcbaDeviceParamType.getPcbaDeviceParamTypeId(), pcbaDeviceParamType.getPcbaDeviceTypeId(), pcbaDeviceParamType.getPcbaDeviceParamTypeName());
                if (pcbaDeviceParamTypeList.size() > 0) {
                    return ResultUtil.error(500, "修改失败，存在相同名字", "Operate Fail", null);
                }

                Integer integer = pcbaDeviceParamTypeDao.updatePcbaDeviceParamType(pcbaDeviceParamType);
                if (integer > 0) {
                    // 操作日志
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(oldPcbaDeviceParamType), JSONObject.toJSONString(pcbaDeviceParamType)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            } else {

                PcbaDeviceParamType byPcbaDeviceTypeIdAndName = pcbaDeviceParamTypeDao.getByPcbaDeviceTypeIdAndName(pcbaDeviceParamType.getPcbaDeviceTypeId(), pcbaDeviceParamType.getPcbaDeviceParamTypeName());
                if (byPcbaDeviceTypeIdAndName != null) {
                    return ResultUtil.error(500, "该类型已存在", "Already Exist", null);
                }

                pcbaDeviceParamType.setCreateTime(new Date());
                Integer integer = pcbaDeviceParamTypeDao.addPcbaDeviceParamType(pcbaDeviceParamType);
                if (integer > 0) {
                    // 操作日志
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 1, 1, new Date(), null, JSONObject.toJSONString(pcbaDeviceParamType)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
